package com.ansatsing.juc.executor.scenario1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) {
        String username = "syq";
        String password = "123345";
        UserValidator ldapValidator = new UserValidator("LDAP");
        UserValidator dbValidator = new UserValidator("DataBase");
        TaskValidator ldapTask = new TaskValidator(ldapValidator,username,password);
        TaskValidator dbTask = new TaskValidator(dbValidator,username,password);
        List<TaskValidator> taskList = new ArrayList<>();
        taskList.add(ldapTask);
        taskList.add(dbTask);

        ExecutorService executor = Executors.newCachedThreadPool();
        String result = "";
        try {
            result = executor.invokeAny(taskList);//返回第一个完成任务的并且没有抛出异常的任务的执行结果
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.printf("Main: Reuslt: %s\n",result);
        executor.shutdown();
        System.out.printf("Main: End of the Execution\n");
    }
}
